import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_roujiaosuo/icons/my_icon.dart';

class RefreshListView extends StatefulWidget {
  RefreshListView(
      {Key key,
      @required this.builder,
      this.connectionState,
      this.itemHeight,
      this.itemCount,
      this.code,
      this.loadMoreBlock,
      this.refreshBlock})
      : super(key: key);
  final double itemHeight;
  final int itemCount;

  final ConnectionState connectionState;
  final String code;
  final IndexedWidgetBuilder builder;
  final refreshBlock;
  final VoidCallback loadMoreBlock;
  _RefreshListViewState createState() => _RefreshListViewState();
}

class _RefreshListViewState extends State<RefreshListView> {
  bool _isRefresh = false;
  bool _isLoadMore = false;
  final controller = ScrollController();

  Future<Null> _refresh() async {
    _isRefresh = true;
    widget.refreshBlock(_isRefresh);
    return null;
  }

  @override
  void initState() {
    super.initState();
    controller.addListener(() {
      if (controller.position.pixels >
          controller.position.maxScrollExtent - 50) {
        if (_isLoadMore == false && widget.itemCount >= 10) {
          _isRefresh = false;
          _isLoadMore = true;
          _refresh();
        }
      }
    });
  }

  Widget _buildeBody() {
    if (widget.connectionState == ConnectionState.none) {
      /// 没有网络

      return RefreshNotNetWorkView(_refresh);
    } else if (widget.connectionState == ConnectionState.active ||
        widget.connectionState == ConnectionState.done) {
      // 有网络
      if (widget.code == '200') {
        _isLoadMore = false;
        return RefreshList(
          controller: controller,
          builder: widget.builder,
          itemCount: widget.itemCount,
          itemHeight: widget.itemHeight,
        );
      } else {
        /// 错误
        return null;
      }
    } else {
      return Container();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: RefreshIndicator(
        onRefresh: _refresh,
        child: _buildeBody(),
      ),
    );
  }
}

class RefreshNotNetWorkView extends StatelessWidget {
  RefreshNotNetWorkView(this.refreshBlock) : super();
  final VoidCallback refreshBlock;

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          Icon(
            MyIcon.my_error_network,
            size: 44,
          ),
          SizedBox(
            height: 20,
          ),
          FlatButton(
              onPressed: this.refreshBlock,
              color: Theme.of(context).primaryColor,
              child: Text('重新加载', style: TextStyle(color: Colors.white)))
        ],
      ),
    );
  }
}

class RefreshList extends StatelessWidget {
  const RefreshList(
      {Key key, this.controller, this.builder, this.itemCount, this.itemHeight})
      : super(key: key);
  final IndexedWidgetBuilder builder;
  final int itemCount;
  final ScrollController controller;
  final double itemHeight;
  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      controller: this.controller,
      itemBuilder: this.builder,
      itemCount: this.itemCount + 1,
      itemExtent: this.itemHeight,
    );
  }
}
